﻿using Sidvall.WorkItemManager.UI.Entities;
using System.Threading.Tasks;

namespace Sidvall.WorkItemManager.UI.ViewModels
{
    public class DashboardViewModel : Sidvall.WorkItemManager.UI.ViewModels.Core.ViewModelBase
    {
        #region LastUpdated

        private System.DateTime? _LastUpdated;
        public System.DateTime? LastUpdated
        {
            get
            {
                return _LastUpdated;
            }
            set
            {
                _LastUpdated = value;
                RaisePropertyChanged("LastUpdated");
            }
        }

        #endregion
        #region LastUpdatedException

        private string _LastUpdatedException;
        public string LastUpdatedException
        {
            get
            {
                return _LastUpdatedException;
            }
            set
            {
                _LastUpdatedException = value;
                RaisePropertyChanged("LastUpdatedException");
                RaisePropertyChanged("LastUpdatedExceptionIsVisible");
            }
        }

        #endregion
        #region LastUpdatedExceptionIsVisible

        public bool LastUpdatedExceptionIsVisible
        {
            get
            {
                if (string.IsNullOrWhiteSpace(this.LastUpdatedException))
                    return false;
                return true;
            }
        }

        #endregion
        #region OverView

        private OverView _OverView;
        public OverView OverView
        {
            get
            {
                return _OverView;
            }
            set
            {
                _OverView = value;
                RaisePropertyChanged("OverView");
            }
        }

        #endregion

        #region ReloadAsync

        public async Task ReloadAsync()
        {
            try
            {
                await this.OverView.ReloadAsync();
                this.LastUpdated = System.DateTime.Now;
                this.LastUpdatedException = null;
            }
            catch (System.Exception ex)
            {
                this.LastUpdatedException = Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(ex);
            }
        }

        #endregion

        #region StartAsync

        protected override async Task StartAsync()
        {
            this.OverView = new OverView();
            await ReloadAsync();
        }

        #endregion
    }
}
